#include "include/ThreadPool.hpp"
#include <unistd.h>
#include <iostream>
using std::cout;
using std::endl;
bool ThreadPool::isExit = false;
ThreadPool::ThreadPool(int thread_cap,int task_cap)
    :capacity(thread_cap),
     task_queue(task_cap)

{
    for(int i =0; i< capacity;++i){
        threadpool.push_back(unique_ptr<Thread>(new workThread(*this)));
    }
}

void ThreadPool::start(){
    for(auto & elem : threadpool){
        elem->start();
    }
}

void ThreadPool::stop(){
    while(!task_queue.empty()){
        sleep(1);
    }
    cout<<"------------current queue size  = "<<task_queue.size()<<endl;
    isExit = true;
    task_queue.wake();
    for(auto & elem : threadpool){
        elem->join();
    }
}

void ThreadPool::addTask(task* tk){
    if(tk!=nullptr){
        task_queue.push(tk);
    }
}

void ThreadPool::doTask(){
    while(!isExit){

    
    auto pt = task_queue.pop();
    if(pt != nullptr){
        pt->process();
    }
    }
}


